<link rel='stylesheet' href='<?php echo base_url();?>css/calendar.css' title='calendar'>

<script language="javascript">

   $(document).ready(function(){
		$('.date-pick').datePicker({startDate:'1996.01.01'});
       $("#category_id").change(function(){
           var category_id = $(this).val();
           $.ajax({
	    	   type: "POST",
	    	   url:"<?php echo  base_url();?>administrator/get_version",
	    	   data: "category_id="+category_id,
	    	   success: function(msg){
	    		   $("#version_id").html("");
		    	   $("#version_id").html(msg);
	    	   }
	    	});
       })

       $("#submit").click(function(){
           var i=0;
            $(".counts").each(function(){
                 if($(this).val()==''){
                	 alert('请核对所有发放份数是否已填写');
                	 i++;
                 }
             })
           var company_id = $("#company_id").val();
            var codes = $("#codes").val();
           if(company_id==""){
                 alert('请选择要发放的单位');
                 return false;
           }

                if(codes==''){
                    alert('请填写发放单号');
                    i++;
                }
          
         
           if(i>0){
              return false;
           }
         
       })
   })
   
   
   function num(va){
	   var cate = $(va).parents("li").parents(".version_ul").parents("li").children(".category_id").val();
       if(cate==""){
 	          alert("请选择类别!");
 	          return false;
       }
   }
   function add(vsa){
//取得型号和类别所选取的值，然后给版本号和数量input 赋name
	  var mod_val = $(vsa).parents(".total").children(".mod").children(".mod_id").val();
	  var cate_val = $(vsa).parents(".version_ul").parents("li").children(".category_id").val();
	   var num = $("#num").val();
	   var n = num.split("_");
	   var i = ++n[1];
	   var na = "version_"+i;
	  if(mod_val==""){
          alert("请选择型号!");
          return false;
	  }
	  if(cate_val==""){
          alert("请选择类别!");
          return false;
	  }
      var str = $(vsa).parents(".version_ul").children("li:eq(1)").html();
      var child = "<li>"+str+"</li>";
      $(vsa).parents(".version_ul").append(child);
      $(vsa).parents(".version_ul").children("li:last-child").children(".version").attr('id',na);
      var num = $("#num").val(na);
   }

   function del(vsa){
	  $(vsa).parents(".version_ul").children("li:eq(2)").remove();
   }	   

   function add_cate(mod){
	   var str = $(mod).parents(".total").children(".category").children("li:eq(0)").html();
	   var m = $(mod).parents(".total").children(".mod").children(".mod_id").val();
       var child = $("#str").val();
       var num = $("#num").val();
	   var n = num.split("_");
	   var i = ++n[1];
	   var na = "version_"+i;
       $(mod).parents(".total").children(".category").append(child);
       if(m==""){
           alert("请选择型号!");
           return false;
 	  }
       $.ajax({
    	   type: "POST",
    	   url:"<?php echo  base_url();?>administrator/get_category",
    	   data: "mod_id="+m,
    	   success: function(msg){
        	   $(mod).parents(".total").children(".category").children("li:last-child").children(".category_id").html("");
        	   $(mod).parents(".total").children(".category").children("li:last-child").children(".category_id").html(msg);
        	   $(mod).parents(".total").children(".category").children("li:last-child").children(".version_ul").children("li:last-child").children(".version").attr('id',na);
     	       var num = $("#num").val(na);
    	   }
    	});
   }

   function del_cate(mod){
	   $(mod).parents(".total").children(".category").children("li:eq(1)").remove();  
   }

   function mod_change(mod){
       var m = $(mod).val();
       var ver_name = "version_"+m+"_0[]";
       var count_name = "counts_"+m+"_0[]";
       $(mod).parents(".total").children(".category").children("li").children(".version_ul").children("li").children(".version").attr('name',ver_name);
       $(mod).parents(".total").children(".category").children("li").children(".version_ul").children("li").children(".counts").attr('name',count_name);  
       $.ajax({
    	   type: "POST",
    	   url:"<?php echo  base_url();?>administrator/get_category",
    	   data: "mod_id="+m,
    	   success: function(msg){
        	   $(mod).parents(".total").children(".category").children("li").children(".category_id").html("");
        	   $(mod).parents(".total").children(".category").children("li").children(".category_id").html(msg);
    	   }
    	});
   }

   function cate_change(cate){
	   var m = $(cate).parents(".category").parents(".total").children(".mod").children(".mod_id").val();
	   var c = $(cate).val();
	   var ver_name = $(cate).parents("li").children(".version_ul").children("li").children(".version").attr('name');
	   var v = ver_name.split("_");
	   var ver_name = "version_"+m+"_"+c+"[]";
	   var count_name = "counts_"+m+"_"+c+"[]";
	   var num = $("#num").val();
	   var n = num.split("_");
	   var i = ++n[1];
	   var na = "version_"+i;
	   
	   var ver1 = $("#ver1").val();
	   var ver2 = $("#ver2").val();
	   var cou1 = $("#cou1").val();
	   var cou2 = $("#cou2").val();
	   var ver = ver1+" name='"+ver_name+"' "+ver2;
	   var cou = ver+cou1+" name='"+count_name+"'"+cou2;

	   
	   $(cate).parents("li").children(".version_ul").children("li:eq(1)").html('');
	   $(cate).parents("li").children(".version_ul").children("li:eq(1)").html(cou);
	   $(cate).parents("li").children(".version_ul").children("li:eq(1)").children(".version").attr('id',na);
       var num = $("#num").val(na);
       $.ajax({
    	   type: "POST",
    	   url:"<?php echo  base_url();?>administrator/get_version_for_send",
    	   data: "mod_id="+m+"&category_id="+c,
    	   success: function(msg){

        	   $(cate).next(".version_ul").children("li").children(".version").html("");
        	   $(cate).next(".version_ul").children("li").children(".version").html(msg);
    	   }
    	});
   }

   function version_change(version){
	   var category_id = $(version).parents(".version_ul").prev("select").val();
	   var version_id = $(version).val();
       $.ajax({
    	   type: "POST",
    	   url:"<?php echo  base_url();?>administrator/get_version_detail",
    	   data: "version_id="+version_id+"&&category_id="+category_id,
    	   success: function(msg){
        	 //库存量  
    		   $(version).next('input').next('span').next('input').val(msg);
	    	   $(version).next('input').next('span').html("<font color='red'>此版本库存"+msg+"份</font>");
	    	 
    	   }
    	});
   }

   function check_num(input){
       var count = $(input).val();
       var inventory = $(input).next('span').next('input').val();
       if(parseInt(count)>parseInt(inventory)){
    	   $(input).next('span').html("<font color='red'>此版本库存"+inventory+"份，请更改！</font>");
    	  
        }else{
        	$(input).next('span').html("<font color='red'>此版本库存"+inventory+"份</font>");
	    	  
	       }
   }

   
</script>
<style>
<!--
  .total{clear:both;}
  .mod,.category{float:left;}
  .category{padding:0 10px 0 10px;margin:0px;}
  .category li{clear:both;}
  li{list-style-type:none;margin:10px 0 10px 0;}
  .version_ul li{margin-top:0px;}
  .counts{width:60px;}
  .category_id,.version_ul{float:left;}
  .del_category,.add_category,.add_version,.del_version,.add_mod,.del_mod{cursor:pointer;}
-->
</style>
<div id="center-column">
			<div class="top-bar">
				<h1>Contents</h1>
				<div class="breadcrumbs"><?php echo anchor('main/index','首页');?> </div>
			</div>
		  <input type="hidden" id="str" value="<li><select name='category_id[]' class='category_id' onchange='cate_change(this)'><option value=''>请选择资料类别</option></select><ul class='version_ul'><li><img src='<?php echo base_url();?>img/hr.gif' class='del_version' onclick='del(this)'/><img src='<?php echo base_url();?>img/add-icon.gif' class='add_version' onclick='add(this)'/></li> <li>版次:<select name='version_0_0[]' class='version' id='version_0' onchange='version_change(this)'><option value=''>请选择资料版本</option></select>	数量：<input type='text' class='counts'  onclick='num(this)' onKeyUp=check_num(this),this.value=this.value.match(/^[0-9]*[1-9][0-9]*$/)==null?'':this.value.match(/^[0-9]*[1-9][0-9]*$/) onafterpaste=this.value=this.value.match(/^[0-9]*[1-9][0-9]*$/)==null?'':this.value.match(/^[0-9]*[1-9][0-9]*$/) name='counts[]' value='' /><span class='current_count'></span><input type='hidden' /></li></ul></li>" />	
		    <input type="hidden" id="num" value="version_0" />
		  <input type="hidden" id="ver1" value="版次:<select " />
		  <input type="hidden" id="ver2" value="id='version_0' class='version' onchange='version_change(this)'><option value=''>请选择资料版本</option></select>" />
		  <input type="hidden" id="cou1" value='数量：<input type="text" class="counts" ' />
		  <input type="hidden" id="cou2"  value="onclick='num(this)' value='' onKeyUp=check_num(this),this.value=this.value.match(/^[0-9]*[1-9][0-9]*$/)==null?'':this.value.match(/^[0-9]*[1-9][0-9]*$/) onafterpaste=this.value=this.value.match(/^[0-9]*[1-9][0-9]*$/)==null?'':this.value.match(/^[0-9]*[1-9][0-9]*$/) /><span class='current_count'></span><input type='hidden' />"	 />
		  <div class="table add_table">
				
				
				
			<?php  echo form_open('administrator/send_add');?>	
			 <div class="receive">
		 
				<div class="total">
				 <div class="mod">型号：<select name="mod_id[]" class="mod_id" onchange="mod_change(this)">
				          <option value="">请选择型号</option>
						 <?php foreach($mod as $k=>$val):?>
						  <option value="<?php echo $val->mod_id;?>" ><?php echo $val->name;?></option>
						 <?php endforeach;?>              
				       </select>
				       <img src="<?php echo base_url();?>img/hr.gif" class="del_category" onclick="del_cate(this)"/>
				       <img src="<?php echo base_url();?>img/add-icon.gif" class="add_category" onclick="add_cate(this)"/><br/><br/>
				       发往单位：<select name="company_id" id="company_id" <?php if(isset($id)){ ?>disabled=true <?php }?>>
						                   <option value="">请选择发往单位</option>
						                   <?php foreach($company as $k=>$val):?>
						                     <option value="<?php echo $val->company_id;?>" <?php if(isset($id)){ if($val->company_id==$send->company_id){?>selected<?php } }?>><?php echo $val->name;?></option>
						                   <?php endforeach;?>
						                 </select>
				  </div>
				       <ul class="category">
				           <li><select name="category_id[]" class="category_id" onchange="cate_change(this)">
				                     <option value="">请选择资料类别</option>
						             </select>						             
						        <ul class="version_ul">
						            <li><img src='<?php echo base_url();?>img/hr.gif' class='del_version' onclick='del(this)'/><img src='<?php echo base_url();?>img/add-icon.gif' class='add_version' onclick='add(this)'/></li> 
						            <li>版次:
								            <select name="version_0_0[]" class="version" id='version_0' onchange="version_change(this)">
						                     <option value="">请选择资料版本</option>
								             </select>	
					                                                     数量：<input type="text" class="counts"  name="counts_0_0[]" onclick="num(this)" value="" onKeyUp="check_num(this),this.value=this.value.match(/^[0-9]*[1-9][0-9]*$/)==null?'':this.value.match(/^[0-9]*[1-9][0-9]*$/)" onafterpaste="this.value=this.value.match(/^[0-9]*[1-9][0-9]*$/)==null?'':this.value.match(/^[0-9]*[1-9][0-9]*$/)" /><span class='current_count'></span><input type='hidden' />					                   
					                    </li>
						        </ul>     
						   </li>
				       </ul> 

				</div>
				
			</div>	
			  <div style="clear:both;padding-top:20px;"><div style="float:left;">发放时间：<input type="text" name="sendtime" value=""   class='date-pick'/>&nbsp;&nbsp;&nbsp;</div><div>发放人员：<input type="text" name="contacter" value="张永军" id="contacter"/>&nbsp;&nbsp;&nbsp;发放单号：<input type="text" name="codes" id="codes"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" id="submit" value="提交" /></div></div>	
			</form>	
				
	        <p>&nbsp;</p>
		  </div>
		</div>
	</div>
	<div id="footer"></div>
</div>



</body>
</html>
